
package com.market.dao;

import java.io.Serializable;
import java.sql.Connection;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class BaseDAO extends HibernateDaoSupport implements IBaseDAO
{
    private static final Logger log = LoggerFactory.getLogger(BaseDAO.class);

    public BaseDAO()
    {
        super();
    }

    public void saveOrUpdate(Object obj)
    {
        log.debug("saving object instance");
        try
        {
            getHibernateTemplate().saveOrUpdate(obj);
            log.debug("save successful");
        }
        catch (RuntimeException re)
        {
            log.error("save failed", re);
            throw re;
        }
    }

    public void delete(Object obj)
    {
        log.debug("deleting object instance");
        try
        {
            getHibernateTemplate().delete(obj);
            log.debug("delete successful");
        }
        catch (RuntimeException re)
        {
            log.error("delete failed", re);
            throw re;
        }
    }

    public List findByDetchedCriteria(DetachedCriteria detachCri, int start, int max)
    {
        if (max != 0)
        {
            return getHibernateTemplate().findByCriteria(detachCri, start, max);
        }
        else
        {
            return getHibernateTemplate().findByCriteria(detachCri);
        }
    }

    public int getCountByDetachedCriteria(DetachedCriteria detachCri)
    {
        detachCri.setProjection(Projections.rowCount());
        List l = getHibernateTemplate().findByCriteria(detachCri);
        if (!l.isEmpty())
        {
            return ((Integer)l.get(0)).intValue();
        }
        else
        {
            return 0;
        }
    }

    public void deleteByDetachedCriteria(DetachedCriteria detachCri)
    {
        getHibernateTemplate().deleteAll(getHibernateTemplate().findByCriteria(detachCri));

    }
    
    public Connection getConnection()
    {
        return getSession().connection();
    }

    public boolean exists(Serializable id)
    {
        // TODO Auto-generated method stub
        return false;
    }

    public Object get(Serializable id)
    {
        // TODO Auto-generated method stub
        return null;
    }

    public List getAll()
    {
        // TODO Auto-generated method stub
        return null;
    }

    public void remove(Serializable id)
    {
        // TODO Auto-generated method stub
        
    }

    public Object save(Object object)
    {
        // TODO Auto-generated method stub
        return null;
    }

}
